package com.cgwx.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * SunTao
 * Date:2017/2/26
 * Time:15:18
 * Email:suntao2808@163.com
 * Description
 */
public class DateUtils {
    /**
     * yyyyMMddHHmmsssss
     */
    public final static String YY_MM_DD_HH_MM_SS_SSS_PATTERN = "yyMMddHHmmsssss";

    public static final String YY_MM_DD_HH_MM_SS = "yy-MM-dd HH-mm-ss";
    /**
     * yyyy-MM-dd
     */
    public final static String YYYY_MM_DD_PATTERN = "yyyy-MM-dd";

    /**
     * yyyy-MM-dd HH:mm:ss
     */
    public final static String YYYY_MM_DD_HH_MM_SS_PATTERN = "yyyy-MM-dd HH:mm:ss";


    private static String DEFAULT_DATE_PATTERN = YYYY_MM_DD_PATTERN;

    private static final int MONTH_PER_YEAR = 12;

    /**
     * yyyy-MM
     */
    public final static String YYYY_MM = "yyyy-MM";

    /**
     * yyyyMM
     */
    public final static String YYYY_MM_PATTERN = "yyyyMM";

    /**
     * yyyy
     */
    public static final String YYYY_PATTERN = "yyyy";

    /**
     * yyyyMMddHHmmsssss
     */
    public final static String YYYY_MM_DD_HH_MM_SS_SSS_PATTERN = "yyyyMMddHHmmsssss";


    public final static String YYYYMMDDHHMMSSPATTERN = "yyyyMMddHHmmss";

    /**
     * 返回自定义格式的当前日期时间字符串
     *
     * @param format 格式规则
     * @return String 返回当前字符串型日期时间
     */
    public static String getCurrentTime(String format) {
        String returnStr = null;
        SimpleDateFormat f = new SimpleDateFormat(format);
        Date date = new Date();
        returnStr = f.format(date);
        return returnStr;
    }

    public static Date stringToDate(String dateString) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse(dateString);
        return date;
    }

    public static Date stringToDateTime(String dateString) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(dateString);
        return date;
    }

    /**
     * @param date
     * @param day  想要获取的日期与传入日期的差值 比如想要获取传入日期前四天的日期 day=-4即可
     * @return
     */
    public static Date getSomeDay(Date date, int day) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.DATE, day);
        return calendar.getTime();
    }

    /**
     * 将长时间格式时间转换为字符串 yyyy-MM-dd HH:mm:ss
     *
     * @param dateDate
     * @return
     */
    public static String dateToStrLong(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    /**
     * 将短时间格式时间转换为字符串 yyyy-MM-dd
     *
     * @param dateDate
     * @return
     */
    public static String dateToStr(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    /**
     * 将短时间格式时间转换为字符串 yyyy年MM月dd日
     *
     * @param dateDate
     * @return
     */
    public static String dateToChineseStr(Date dateDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        String dateString = formatter.format(dateDate);
        return dateString;
    }

    public static int getDaysOfMonth(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    public static Date stringToMonth(String monthString) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Date date = sdf.parse(monthString);
        return date;
    }


    // 过去7天
    public static String lastSevenDaysString(Date dateDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateDate);
        calendar.add(Calendar.DATE, -7);
        Date date = calendar.getTime();
        String dateString = dateToStr(date);
        return dateString;
    }

    // 过去一个月
    public static String lastMonthDayString(Date dateDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateDate);
        calendar.add(Calendar.MONTH, -1);
        Date date = calendar.getTime();
        String dateString = dateToStr(date);
        return dateString;
    }

    //过去三个月
    public static String lastThreeMonthDayString(Date dateDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateDate);
        calendar.add(Calendar.MONTH, -3);
        Date date = calendar.getTime();
        String dateString = dateToStr(date);
        return dateString;
    }

    //过去一年
    public static String lastYearDayString(Date dateDate) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateDate);
        calendar.add(Calendar.YEAR, -1);
        Date date = calendar.getTime();
        String dateString = dateToStrLong(date);
        return dateString;
    }

    // 两个日期之间的天数
    public static Integer daysBetweenTwoDate(Date startDate,Date endDate)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(startDate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(endDate);
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);
        return Integer.parseInt(String.valueOf(between_days));
    }




}
